package com.kly.manager.websocket;

import cn.hutool.core.util.StrUtil;
import com.kly.domain.entity.Picture;
import com.kly.domain.entity.Space;
import com.kly.domain.entity.User;
import com.kly.manager.auth.SpaceUserAuthManage;
import com.kly.manager.auth.model.SpaceUserPermissionConstant;
import com.kly.service.PictureService;
import com.kly.service.SpaceService;
import com.kly.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author admin
 * @date 2025/3/21
 */
@Slf4j
@Configuration
public class WsHandShakeInterceptor implements HandshakeInterceptor {

    @Resource
    private UserService userService;
    @Resource
    private PictureService pictureService;
    @Resource
    private SpaceService spaceService;
    @Resource
    private SpaceUserAuthManage spaceUserAuthManage;

    @Override
    public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {
        // 1. 判断请求参数
        if (request instanceof ServletServerHttpRequest) {
            HttpServletRequest httpServletRequest = ((ServletServerHttpRequest) request).getServletRequest();
            //     2. 判断用户是否有权限
            User loginUser = userService.getLoginUser(httpServletRequest);
            if (loginUser == null) {
                log.error("用户未登录");
                return false;
            }
            //     获取图片id
            String pictureId = httpServletRequest.getParameter("pictureId");
            if (StrUtil.isBlank(pictureId)) {
                log.error("pictureId不能为空");
                return false;
            }
            //     校验权限
            Long pictureIdLong = Long.parseLong(pictureId);
            Picture pictureDb = pictureService.getById(pictureIdLong);
            if (pictureDb == null) {
                log.error("图片不存在");
                return false;
            }
            //     获取用户权限
            Long spaceId = pictureDb.getSpaceId();
            Space spaceDb = spaceService.getById(spaceId);
            if (spaceDb == null) {
                log.error("空间不存在");
                return false;
            }
            List<String> permissionList = spaceUserAuthManage.getPermissionList(spaceDb, loginUser);
            if (!permissionList.contains(SpaceUserPermissionConstant.PICTURE_EDIT)) {
                log.error("用户无权限");
                return false;
            }

            //     3. 设置attributes
            attributes.put("loginUser", loginUser);
            attributes.put("pictureId", pictureIdLong);
            attributes.put("userId", loginUser.getId());
        }

        return true;
    }

    @Override
    public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception exception) {

    }
}
